package com.gt.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.QualityModel;
import com.gt.admin.domain.apiVO.Version;
import com.gt.admin.domain.vo.QualityModelVO;
import com.gt.admin.mapper.QualityItemMapper;
import com.gt.admin.mapper.QualityModelMapper;
import com.gt.admin.service.IQualityModelService;
import com.gt.admin.utils.Effect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 质控方案service
 */
@Service
public class QualityModelServiceImpl extends ServiceImpl<QualityModelMapper, QualityModel> implements IQualityModelService {
    @Autowired
    private QualityModelMapper qualityModelMapper;
    @Autowired
    private QualityItemMapper qualityItemMapper;

    /**
     * 按superId查询版本
     */
    @Override
    public List<Version> selectVersion(Long superId) {
        return qualityModelMapper.selectVersion(superId);
    }

    /**
     * 按superId查询质控方案
     */
    @Override
    public QualityModelVO selectBySuperId(Long superId) {
        LambdaQueryWrapper<QualityModel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(QualityModel::getSuperId, superId);
        List<QualityModel> list = list(queryWrapper);
        QualityModelVO qualityModelVO = new QualityModelVO();
        qualityModelVO.setQualityModelList(list);
        for (QualityModel model : list) {
            if (model.getEffect() == Effect.Effective){
                qualityModelVO.setChooseVersion(model.getVersion());
            }
        }
        return qualityModelVO;
    }

    /**
     * 按id删除质控方案及对应指标
     */
    @Transactional
    @Override
    public boolean deleteById(Long quaId) {
        boolean flag = removeById(quaId);
        Long[] itemIds = qualityItemMapper.selectByQuaId(quaId);
        qualityItemMapper.deleteBatchIds(Arrays.asList(itemIds));
        return flag;
    }

    /**
     * 新增或修改质控方案，更新生效版本
     */
    @Override
    public boolean updateByVersion(QualityModelVO qualityModelVO) {
        boolean flag = false;
        Long version = qualityModelVO.getChooseVersion();
        List<QualityModel> qualityModelList = qualityModelVO.getQualityModelList();
        Long superId = qualityModelList.get(1).getSuperId();
        LambdaUpdateWrapper<QualityModel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(QualityModel::getEffect, Effect.Invalid)
                .eq(QualityModel::getSuperId, superId);
        update(updateWrapper);
        for (QualityModel model : qualityModelList) {
            if(model.getVersion().longValue() == version.longValue()){
                model.setEffect(Effect.Effective);
                flag = saveOrUpdate(model);
            }
        }return flag;
    }
}
